// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.areas.isoregion.data;

import java.nio.ByteBuffer;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import zombie.debug.DebugLog;
import zombie.iso.areas.isoregion.IsoRegions;
import zombie.iso.areas.isoregion.regions.IsoChunkRegion;
import zombie.iso.areas.isoregion.regions.IsoWorldRegion;

public final class DataChunk {
    private final DataCell cell;
    private final int hashId;
    private final int chunkX;
    private final int chunkY;
    protected int highestZ = 0;
    protected long lastUpdateStamp = 0L;
    private final boolean[] activeZLayers = new boolean[8];
    private final boolean[] dirtyZLayers = new boolean[8];
    private byte[] squareFlags;
    private byte[] regionIDs;
    private final ArrayList<ArrayList<IsoChunkRegion>> chunkRegions = new ArrayList(8);
    private static byte selectedFlags;
    private static final ArrayDeque<DataSquarePos> tmpSquares = new ArrayDeque();
    private static final HashSet<Integer> tmpLinkedChunks = new HashSet();
    private static final boolean[] exploredPositions = new boolean[100];
    private static IsoChunkRegion lastCurRegion;
    private static IsoChunkRegion lastOtherRegionFullConnect;
    private static ArrayList<IsoChunkRegion> oldList = new ArrayList();
    private static final ArrayDeque<IsoChunkRegion> chunkQueue = new ArrayDeque();

    protected DataChunk(int int1, int int2, DataCell dataCell, int int0) {
        this.cell = dataCell;
        this.hashId = int0 < 0 ? IsoRegions.hash(int1, int2) : int0;
        this.chunkX = int1;
        this.chunkY = int2;

        for (int int3 = 0; int3 < 8; int3++) {
            this.chunkRegions.add(new ArrayList());
        }
    }

    protected int getHashId() {
        return this.hashId;
    }

    public int getChunkX() {
        return this.chunkX;
    }

    public int getChunkY() {
        return this.chunkY;
    }

    protected ArrayList<IsoChunkRegion> getChunkRegions(int _int) {
        return (ArrayList<IsoChunkRegion>)this.chunkRegions.get(_int);
    }

    public long getLastUpdateStamp() {
        return this.lastUpdateStamp;
    }

    public void setLastUpdateStamp(long _lastUpdateStamp) {
        this.lastUpdateStamp = _lastUpdateStamp;
    }

    protected boolean isDirty(int _int) {
        return this.activeZLayers[_int] ? this.dirtyZLayers[_int] : false;
    }

    protected void setDirty(int _int) {
        if (this.activeZLayers[_int]) {
            this.dirtyZLayers[_int] = true;
            this.cell.dataRoot.EnqueueDirtyDataChunk(this);
        }
    }

    public void setDirtyAllActive() {
        boolean _boolean = false;

        for (int _int = 0; _int < 8; _int++) {
            if (this.activeZLayers[_int]) {
                this.dirtyZLayers[_int] = true;
                if (!_boolean) {
                    this.cell.dataRoot.EnqueueDirtyDataChunk(this);
                    _boolean = true;
                }
            }
        }
    }

    protected void unsetDirtyAll() {
        for (int _int = 0; _int < 8; _int++) {
            this.dirtyZLayers[_int] = false;
        }
    }

    private boolean validCoords(int int2, int int1, int int0) {
        return int2 >= 0 && int2 < 10 && int1 >= 0 && int1 < 10 && int0 >= 0 && int0 < this.highestZ + 1;
    }

    private int getCoord1D(int int0, int int1, int int2) {
        return int2 * 10 * 10 + int1 * 10 + int0;
    }

    public byte getSquare(int x, int y, int z) {
        return this.getSquare(x, y, z, false);
    }

    public byte getSquare(int x, int y, int z, boolean ignoreCoordCheck) {
        if (this.squareFlags != null && (ignoreCoordCheck || this.validCoords(x, y, z))) {
            return this.activeZLayers[z] ? this.squareFlags[this.getCoord1D(x, y, z)] : -1;
        } else {
            return -1;
        }
    }

    protected byte setOrAddSquare(int int0, int int1, int int2, byte _byte) {
        return this.setOrAddSquare(int0, int1, int2, _byte, false);
    }

    protected byte setOrAddSquare(int int0, int int1, int int2, byte _byte, boolean _boolean) {
        if (!_boolean && !this.validCoords(int0, int1, int2)) {
            return -1;
        } else {
            this.ensureSquares(int2);
            int int3 = this.getCoord1D(int0, int1, int2);
            if (this.squareFlags[int3] != _byte) {
                this.setDirty(int2);
            }

            this.squareFlags[int3] = _byte;
            return _byte;
        }
    }

    private void ensureSquares(int int0) {
        if (int0 >= 0 && int0 < 8) {
            if (!this.activeZLayers[int0]) {
                this.ensureSquareArray(int0);
                this.activeZLayers[int0] = true;
                if (int0 > this.highestZ) {
                    this.highestZ = int0;
                }

                for (int int1 = 0; int1 < 10; int1++) {
                    for (int int2 = 0; int2 < 10; int2++) {
                        int int3 = this.getCoord1D(int2, int1, int0);
                        this.squareFlags[int3] = (byte)(int0 == 0 ? 16 : 0);
                    }
                }
            }
        }
    }

    private void ensureSquareArray(int int1) {
        int int0 = (int1 + 1) * 10 * 10;
        if (this.squareFlags == null || this.squareFlags.length < int0) {
            byte[] byte0 = this.squareFlags;
            byte[] byte1 = this.regionIDs;
            this.squareFlags = new byte[int0];
            this.regionIDs = new byte[int0];
            if (byte0 != null) {
                for (int int2 = 0; int2 < byte0.length; int2++) {
                    this.squareFlags[int2] = byte0[int2];
                    this.regionIDs[int2] = byte1[int2];
                }
            }
        }
    }

    public void save(ByteBuffer bb) {
        try {
            int int0 = bb.position();
            bb.putInt(0);
            bb.putInt(this.highestZ);
            int int1 = (this.highestZ + 1) * 100;
            bb.putInt(int1);

            for (int int2 = 0; int2 < int1; int2++) {
                bb.put(this.squareFlags[int2]);
            }

            int int3 = bb.position();
            bb.position(int0);
            bb.putInt(int3 - int0);
            bb.position(int3);
        } catch (Exception exception) {
            DebugLog.log(exception.getMessage());
            exception.printStackTrace();
        }
    }

    public void load(ByteBuffer bb, int worldVersion, boolean readLength) {
        try {
            if (readLength) {
                bb.getInt();
            }

            this.highestZ = bb.getInt();

            for (int int0 = this.highestZ; int0 >= 0; int0--) {
                this.ensureSquares(int0);
            }

            int int1 = bb.getInt();

            for (int int2 = 0; int2 < int1; int2++) {
                this.squareFlags[int2] = bb.get();
            }
        } catch (Exception exception) {
            DebugLog.log(exception.getMessage());
            exception.printStackTrace();
        }
    }

    public void setSelectedFlags(int x, int y, int z) {
        if (z >= 0 && z <= this.highestZ) {
            selectedFlags = this.squareFlags[this.getCoord1D(x, y, z)];
        } else {
            selectedFlags = -1;
        }
    }

    public boolean selectedHasFlags(byte flags) {
        return (selectedFlags & flags) == flags;
    }

    protected boolean squareHasFlags(int int0, int int1, int int2, byte _byte) {
        return this.squareHasFlags(this.getCoord1D(int0, int1, int2), _byte);
    }

    private boolean squareHasFlags(int _int, byte byte1) {
        byte byte0 = this.squareFlags[_int];
        return (byte0 & byte1) == byte1;
    }

    public byte squareGetFlags(int x, int y, int z) {
        return this.squareGetFlags(this.getCoord1D(x, y, z));
    }

    private byte squareGetFlags(int _int) {
        return this.squareFlags[_int];
    }

    protected void squareAddFlags(int int0, int int1, int int2, byte _byte) {
        this.squareAddFlags(this.getCoord1D(int0, int1, int2), _byte);
    }

    private void squareAddFlags(int _int, byte _byte) {
        this.squareFlags[_int] = (byte)(this.squareFlags[_int] | _byte);
    }

    protected void squareRemoveFlags(int int0, int int1, int int2, byte _byte) {
        this.squareRemoveFlags(this.getCoord1D(int0, int1, int2), _byte);
    }

    private void squareRemoveFlags(int _int, byte _byte) {
        this.squareFlags[_int] = (byte)(this.squareFlags[_int] ^ _byte);
    }

    protected boolean squareCanConnect(int int1, int int2, int int0, byte _byte) {
        return this.squareCanConnect(this.getCoord1D(int1, int2, int0), int0, _byte);
    }

    private boolean squareCanConnect(int int1, int int0, byte _byte) {
        if (int0 >= 0 && int0 < this.highestZ + 1) {
            if (_byte == 0) {
                return !this.squareHasFlags(int1, (byte)1);
            }

            if (_byte == 1) {
                return !this.squareHasFlags(int1, (byte)2);
            }

            if (_byte == 2) {
                return true;
            }

            if (_byte == 3) {
                return true;
            }

            if (_byte == 4) {
                return !this.squareHasFlags(int1, (byte)64);
            }

            if (_byte == 5) {
                return !this.squareHasFlags(int1, (byte)16);
            }
        }

        return false;
    }

    public IsoChunkRegion getIsoChunkRegion(int x, int y, int z) {
        return this.getIsoChunkRegion(this.getCoord1D(x, y, z), z);
    }

    private IsoChunkRegion getIsoChunkRegion(int int1, int int0) {
        if (int0 >= 0 && int0 < this.highestZ + 1) {
            byte _byte = this.regionIDs[int1];
            if (_byte >= 0 && _byte < ((ArrayList)this.chunkRegions.get(int0)).size()) {
                return (IsoChunkRegion)((ArrayList)this.chunkRegions.get(int0)).get(_byte);
            }
        }

        return null;
    }

    public void setRegion(int x, int y, int z, byte regionIndex) {
        this.regionIDs[this.getCoord1D(x, y, z)] = regionIndex;
    }

    protected void recalculate() {
        for (int _int = 0; _int <= this.highestZ; _int++) {
            if (this.dirtyZLayers[_int] && this.activeZLayers[_int]) {
                this.recalculate(_int);
            }
        }
    }

    private void recalculate(int int0) {
        ArrayList arrayList = (ArrayList)this.chunkRegions.get(int0);

        for (int int1 = arrayList.size() - 1; int1 >= 0; int1--) {
            IsoChunkRegion isoChunkRegion0 = (IsoChunkRegion)arrayList.get(int1);
            IsoWorldRegion isoWorldRegion = isoChunkRegion0.unlinkFromIsoWorldRegion();
            if (isoWorldRegion != null && isoWorldRegion.size() <= 0) {
                this.cell.dataRoot.regionManager.releaseIsoWorldRegion(isoWorldRegion);
            }

            this.cell.dataRoot.regionManager.releaseIsoChunkRegion(isoChunkRegion0);
            arrayList.remove(int1);
        }

        arrayList.clear();
        byte _byte = 100;
        Arrays.fill(this.regionIDs, int0 * _byte, int0 * _byte + _byte, (byte)-1);

        for (int int2 = 0; int2 < 10; int2++) {
            for (int int3 = 0; int3 < 10; int3++) {
                if (this.regionIDs[this.getCoord1D(int3, int2, int0)] == -1) {
                    IsoChunkRegion isoChunkRegion1 = this.floodFill(int3, int2, int0);
                }
            }
        }
    }

    private IsoChunkRegion floodFill(int int1, int int2, int int0) {
        IsoChunkRegion isoChunkRegion0 = this.cell.dataRoot.regionManager.allocIsoChunkRegion(int0);
        byte byte0 = (byte)((ArrayList)this.chunkRegions.get(int0)).size();
        ((ArrayList)this.chunkRegions.get(int0)).add(isoChunkRegion0);
        this.clearExploredPositions();
        tmpSquares.clear();
        tmpLinkedChunks.clear();
        tmpSquares.add(DataSquarePos.alloc(int1, int2, int0));

        DataSquarePos dataSquarePos0;
        while ((dataSquarePos0 = (DataSquarePos)tmpSquares.poll()) != null) {
            int int3 = this.getCoord1D(dataSquarePos0.x, dataSquarePos0.y, dataSquarePos0.z);
            this.setExploredPosition(int3, dataSquarePos0.z);
            if (this.regionIDs[int3] == -1) {
                this.regionIDs[int3] = byte0;
                isoChunkRegion0.addSquareCount();

                for (byte byte1 = 0; byte1 < 4; byte1++) {
                    DataSquarePos dataSquarePos1 = this.getNeighbor(dataSquarePos0, byte1);
                    if (dataSquarePos1 != null) {
                        int int4 = this.getCoord1D(dataSquarePos1.x, dataSquarePos1.y, dataSquarePos1.z);
                        if (this.isExploredPosition(int4, dataSquarePos1.z)) {
                            DataSquarePos.release(dataSquarePos1);
                        } else {
                            if (this.squareCanConnect(int3, dataSquarePos0.z, byte1)
                                && this.squareCanConnect(int4, dataSquarePos1.z, IsoRegions.GetOppositeDir(byte1))) {
                                if (this.regionIDs[int4] == -1) {
                                    tmpSquares.add(dataSquarePos1);
                                    this.setExploredPosition(int4, dataSquarePos1.z);
                                    continue;
                                }
                            } else {
                                IsoChunkRegion isoChunkRegion1 = this.getIsoChunkRegion(int4, dataSquarePos1.z);
                                if (isoChunkRegion1 != null && isoChunkRegion1 != isoChunkRegion0) {
                                    if (!tmpLinkedChunks.contains(isoChunkRegion1.getID())) {
                                        isoChunkRegion0.addNeighbor(isoChunkRegion1);
                                        isoChunkRegion1.addNeighbor(isoChunkRegion0);
                                        tmpLinkedChunks.add(isoChunkRegion1.getID());
                                    }

                                    this.setExploredPosition(int4, dataSquarePos1.z);
                                    DataSquarePos.release(dataSquarePos1);
                                    continue;
                                }
                            }

                            DataSquarePos.release(dataSquarePos1);
                        }
                    } else if (this.squareCanConnect(int3, dataSquarePos0.z, byte1)) {
                        isoChunkRegion0.addChunkBorderSquaresCnt();
                    }
                }
            }
        }

        return isoChunkRegion0;
    }

    private boolean isExploredPosition(int int1, int int2) {
        int int0 = int1 - int2 * 10 * 10;
        return exploredPositions[int0];
    }

    private void setExploredPosition(int int1, int int2) {
        int int0 = int1 - int2 * 10 * 10;
        exploredPositions[int0] = true;
    }

    private void clearExploredPositions() {
        Arrays.fill(exploredPositions, false);
    }

    private DataSquarePos getNeighbor(DataSquarePos dataSquarePos, byte _byte) {
        int int0 = dataSquarePos.x;
        int int1 = dataSquarePos.y;
        if (_byte == 1) {
            int0 = dataSquarePos.x - 1;
        } else if (_byte == 3) {
            int0 = dataSquarePos.x + 1;
        }

        if (_byte == 0) {
            int1 = dataSquarePos.y - 1;
        } else if (_byte == 2) {
            int1 = dataSquarePos.y + 1;
        }

        return int0 >= 0 && int0 < 10 && int1 >= 0 && int1 < 10 ? DataSquarePos.alloc(int0, int1, dataSquarePos.z) : null;
    }

    protected void link(DataChunk dataChunk1, DataChunk dataChunk2, DataChunk dataChunk3, DataChunk dataChunk4) {
        for (int _int = 0; _int <= this.highestZ; _int++) {
            if (this.dirtyZLayers[_int] && this.activeZLayers[_int]) {
                this.linkRegionsOnSide(_int, dataChunk1, (byte)0);
                this.linkRegionsOnSide(_int, dataChunk2, (byte)1);
                this.linkRegionsOnSide(_int, dataChunk3, (byte)2);
                this.linkRegionsOnSide(_int, dataChunk4, (byte)3);
            }
        }
    }

    private void linkRegionsOnSide(int int4, DataChunk dataChunk0, byte _byte) {
        int int0;
        int int1;
        int int2;
        int int3;
        if (_byte != 0 && _byte != 2) {
            int0 = _byte == 1 ? 0 : 9;
            int1 = int0 + 1;
            int2 = 0;
            int3 = 10;
        } else {
            int0 = 0;
            int1 = 10;
            int2 = _byte == 0 ? 0 : 9;
            int3 = int2 + 1;
        }

        if (dataChunk0 != null && dataChunk0.isDirty(int4)) {
            dataChunk0.resetEnclosedSide(int4, IsoRegions.GetOppositeDir(_byte));
        }

        lastCurRegion = null;
        lastOtherRegionFullConnect = null;

        for (int int5 = int2; int5 < int3; int5++) {
            for (int int6 = int0; int6 < int1; int6++) {
                int int7;
                int int8;
                if (_byte != 0 && _byte != 2) {
                    int7 = _byte == 1 ? 9 : 0;
                    int8 = int5;
                } else {
                    int7 = int6;
                    int8 = _byte == 0 ? 9 : 0;
                }

                int int9 = this.getCoord1D(int6, int5, int4);
                int int10 = this.getCoord1D(int7, int8, int4);
                IsoChunkRegion isoChunkRegion0 = this.getIsoChunkRegion(int9, int4);
                IsoChunkRegion isoChunkRegion1 = dataChunk0 != null ? dataChunk0.getIsoChunkRegion(int10, int4) : null;
                if (isoChunkRegion0 == null) {
                    IsoRegions.warn("ds.getRegion()==null, shouldnt happen at this point.");
                } else {
                    if (lastCurRegion != null && lastCurRegion != isoChunkRegion0) {
                        lastOtherRegionFullConnect = null;
                    }

                    if (lastCurRegion == null || lastCurRegion != isoChunkRegion0 || isoChunkRegion1 == null || lastOtherRegionFullConnect != isoChunkRegion1) {
                        if (dataChunk0 != null && isoChunkRegion1 != null) {
                            if (this.squareCanConnect(int9, int4, _byte) && dataChunk0.squareCanConnect(int10, int4, IsoRegions.GetOppositeDir(_byte))) {
                                isoChunkRegion0.addConnectedNeighbor(isoChunkRegion1);
                                isoChunkRegion1.addConnectedNeighbor(isoChunkRegion0);
                                isoChunkRegion0.addNeighbor(isoChunkRegion1);
                                isoChunkRegion1.addNeighbor(isoChunkRegion0);
                                if (!isoChunkRegion1.getIsEnclosed()) {
                                    isoChunkRegion1.setEnclosed(IsoRegions.GetOppositeDir(_byte), true);
                                }

                                lastOtherRegionFullConnect = isoChunkRegion1;
                            } else {
                                isoChunkRegion0.addNeighbor(isoChunkRegion1);
                                isoChunkRegion1.addNeighbor(isoChunkRegion0);
                                if (!isoChunkRegion1.getIsEnclosed()) {
                                    isoChunkRegion1.setEnclosed(IsoRegions.GetOppositeDir(_byte), true);
                                }

                                lastOtherRegionFullConnect = null;
                            }
                        } else if (this.squareCanConnect(int9, int4, _byte)) {
                            isoChunkRegion0.setEnclosed(_byte, false);
                        }

                        lastCurRegion = isoChunkRegion0;
                    }
                }
            }
        }
    }

    private void resetEnclosedSide(int int0, byte _byte) {
        ArrayList arrayList = (ArrayList)this.chunkRegions.get(int0);

        for (int int1 = 0; int1 < arrayList.size(); int1++) {
            IsoChunkRegion isoChunkRegion = (IsoChunkRegion)arrayList.get(int1);
            if (isoChunkRegion.getzLayer() == int0) {
                isoChunkRegion.setEnclosed(_byte, true);
            }
        }
    }

    protected void interConnect() {
        for (int int0 = 0; int0 <= this.highestZ; int0++) {
            if (this.dirtyZLayers[int0] && this.activeZLayers[int0]) {
                ArrayList arrayList = (ArrayList)this.chunkRegions.get(int0);

                for (int int1 = 0; int1 < arrayList.size(); int1++) {
                    IsoChunkRegion isoChunkRegion0 = (IsoChunkRegion)arrayList.get(int1);
                    if (isoChunkRegion0.getzLayer() == int0 && isoChunkRegion0.getIsoWorldRegion() == null) {
                        if (isoChunkRegion0.getConnectedNeighbors().size() == 0) {
                            IsoWorldRegion isoWorldRegion0 = this.cell.dataRoot.regionManager.allocIsoWorldRegion();
                            this.cell.dataRoot.EnqueueDirtyIsoWorldRegion(isoWorldRegion0);
                            isoWorldRegion0.addIsoChunkRegion(isoChunkRegion0);
                        } else {
                            IsoChunkRegion isoChunkRegion1 = isoChunkRegion0.getConnectedNeighborWithLargestIsoWorldRegion();
                            if (isoChunkRegion1 == null) {
                                IsoWorldRegion isoWorldRegion1 = this.cell.dataRoot.regionManager.allocIsoWorldRegion();
                                this.cell.dataRoot.EnqueueDirtyIsoWorldRegion(isoWorldRegion1);
                                this.floodFillExpandWorldRegion(isoChunkRegion0, isoWorldRegion1);
                                DataRoot.floodFills++;
                            } else {
                                IsoWorldRegion isoWorldRegion2 = isoChunkRegion1.getIsoWorldRegion();
                                oldList.clear();
                                oldList = isoWorldRegion2.swapIsoChunkRegions(oldList);

                                for (int int2 = 0; int2 < oldList.size(); int2++) {
                                    IsoChunkRegion isoChunkRegion2 = (IsoChunkRegion)oldList.get(int2);
                                    isoChunkRegion2.setIsoWorldRegion(null);
                                }

                                this.cell.dataRoot.regionManager.releaseIsoWorldRegion(isoWorldRegion2);
                                IsoWorldRegion isoWorldRegion3 = this.cell.dataRoot.regionManager.allocIsoWorldRegion();
                                this.cell.dataRoot.EnqueueDirtyIsoWorldRegion(isoWorldRegion3);
                                this.floodFillExpandWorldRegion(isoChunkRegion0, isoWorldRegion3);

                                for (int int3 = 0; int3 < oldList.size(); int3++) {
                                    IsoChunkRegion isoChunkRegion3 = (IsoChunkRegion)oldList.get(int3);
                                    if (isoChunkRegion3.getIsoWorldRegion() == null) {
                                        IsoWorldRegion isoWorldRegion4 = this.cell.dataRoot.regionManager.allocIsoWorldRegion();
                                        this.cell.dataRoot.EnqueueDirtyIsoWorldRegion(isoWorldRegion4);
                                        this.floodFillExpandWorldRegion(isoChunkRegion3, isoWorldRegion4);
                                    }
                                }

                                DataRoot.floodFills++;
                            }
                        }
                    }
                }
            }
        }
    }

    private void floodFillExpandWorldRegion(IsoChunkRegion isoChunkRegion0, IsoWorldRegion isoWorldRegion) {
        chunkQueue.add(isoChunkRegion0);

        IsoChunkRegion isoChunkRegion1;
        while ((isoChunkRegion1 = (IsoChunkRegion)chunkQueue.poll()) != null) {
            isoWorldRegion.addIsoChunkRegion(isoChunkRegion1);
            if (isoChunkRegion1.getConnectedNeighbors().size() != 0) {
                for (int _int = 0; _int < isoChunkRegion1.getConnectedNeighbors().size(); _int++) {
                    IsoChunkRegion isoChunkRegion2 = (IsoChunkRegion)isoChunkRegion1.getConnectedNeighbors().get(_int);
                    if (!chunkQueue.contains(isoChunkRegion2)) {
                        if (isoChunkRegion2.getIsoWorldRegion() == null) {
                            chunkQueue.add(isoChunkRegion2);
                        } else if (isoChunkRegion2.getIsoWorldRegion() != isoWorldRegion) {
                            isoWorldRegion.merge(isoChunkRegion2.getIsoWorldRegion());
                        }
                    }
                }
            }
        }
    }

    protected void recalcRoofs() {
        if (this.highestZ >= 1) {
            for (int int0 = 0; int0 < this.chunkRegions.size(); int0++) {
                for (int int1 = 0; int1 < ((ArrayList)this.chunkRegions.get(int0)).size(); int1++) {
                    IsoChunkRegion isoChunkRegion0 = (IsoChunkRegion)((ArrayList)this.chunkRegions.get(int0)).get(int1);
                    isoChunkRegion0.resetRoofCnt();
                }
            }

            int int2 = this.highestZ;

            for (int int3 = 0; int3 < 10; int3++) {
                for (int int4 = 0; int4 < 10; int4++) {
                    byte _byte = this.getSquare(int4, int3, int2);
                    boolean _boolean = false;
                    if (_byte > 0) {
                        _boolean = this.squareHasFlags(int4, int3, int2, (byte)16);
                    }

                    if (int2 >= 1) {
                        for (int int5 = int2 - 1; int5 >= 0; int5--) {
                            _byte = this.getSquare(int4, int3, int5);
                            if (_byte > 0) {
                                _boolean = _boolean || this.squareHasFlags(int4, int3, int5, (byte)32);
                                if (_boolean) {
                                    IsoChunkRegion isoChunkRegion1 = this.getIsoChunkRegion(int4, int3, int5);
                                    if (isoChunkRegion1 != null) {
                                        isoChunkRegion1.addRoof();
                                        if (isoChunkRegion1.getIsoWorldRegion() != null && !isoChunkRegion1.getIsoWorldRegion().isEnclosed()) {
                                            _boolean = false;
                                        }
                                    } else {
                                        _boolean = false;
                                    }
                                }

                                if (!_boolean) {
                                    _boolean = this.squareHasFlags(int4, int3, int5, (byte)16);
                                }
                            } else {
                                _boolean = false;
                            }
                        }
                    }
                }
            }
        }
    }
}
